<?php


namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\SchoolproductModel;
use app\common\model\ProductModel;
use think\facade\Db;
use think\facade\Session;
class Schoolproduct extends AdminBase
{
    protected SchoolproductModel $model;

    /**
     * 初始化
     * @return void
     */
    public function initialize()
    {
        parent::initialize();
        $this->model = new SchoolproductModel();
		$this->models = new ProductModel();
    }

    /**
     * 学校产品列表
     */
    public function index()
    {
        $param = $this->request->param();
        $model = $this->model;
        //构造搜索条件
        
        if( $param['start_date']!=''&&$param['end_date']!='' ) {
            $start_date = $param['start_date'].' 00:00:00';
            $end_date = $param['end_date'].' 23:59:59';
            $model = $model->whereBetweenTime('create_time',$start_date,$end_date);
        }
		if( $param['name']!='' ) {
		    $model = $model->whereLike('b.name',"%".$param['name']."%");
		}
		if( $param['typeid']!='' ) {
		    $model = $model->where('b.typeid',$param['typeid']);
		}
		if( $param['category_id']!='' ) {
			$model= $model->where('b.category_id',$param['category_id']);
		}
		if( $param['subject_id']!='' ) {
			 $model = $model->where('b.subject_id',$param['subject_id']);
		}
		$admin=Session::get('admin_auth');
		$info=Db::name('admin')->where(['id'=>$admin['id']])->find();
		$result=Db::name('auth_group_access')->where(['admin_id'=>$admin['id']])->find();
		if($result['group_id']==1){
		$list = $model->order('id desc')
							->alias('a')
							->join('product b','a.product_id=b.id')
							->join('shoptype c','c.id=b.typeid')
							->join('shopcategory d','d.id=b.category_id')
							->join('shopsubject e','e.id=b.subject_id')
							->field('a.*,b.name as proname,c.name as typename,d.title as category_name,e.title as subject_name')
						    ->paginate([
						        'query' => $param,
						        'list_rows'=>$this->request->param('limit',20)
						    ]);
	   }else{
		   $list = $model->order('id desc')
		   					->alias('a')
		   					->join('product b','a.product_id=b.id')
		   					->join('shoptype c','c.id=b.typeid')
		   					->join('shopcategory d','d.id=b.category_id')
		   					->join('shopsubject e','e.id=b.subject_id')
		   					->field('a.*,b.name as proname,c.name as typename,d.title as category_name,e.title as subject_name')
							->where(['a.platform_id'=>$info['type_id']])
		   				    ->paginate([
		   				        'query' => $param,
		   				        'list_rows'=>$this->request->param('limit',20)
		   				    ]);
	   }
        // $list = $model->order('id desc')
        //     ->paginate([
        //         'query' => $param,
        //         'list_rows'=>$this->request->param('limit',20)
        //     ]);
        // return view('', compact('list'));
		$type=Db::name('shoptype')->where('status',1)->order('weigh asc,create_time desc')->select()->toArray();
		$category=Db::name('shopcategory')->where('status',1)->order('weigh asc,create_time desc')->select()->toArray();
		$subject=Db::name('shopsubject')->where('status',1)->order('weigh asc,create_time desc')->select()->toArray();
		return view('',compact('type','category','subject','list'));
    }
	//显示平台产品
    public function category(){
		$models = $this->models;
		$param = $this->request->param();
		// print_r($param);exit;
		if( $param['name']!='' ) {
		    $models = $models->whereLike('a.name',"%".$param['name']."%");
		}
		if( $param['typeid']!='' ) {
		    $models = $models->where('a.typeid',$param['typeid']);
		}
		if( $param['category_id']!='' ) {
			$models = $models->where('a.category_id',$param['category_id']);
		}
		if( $param['subject_id']!='' ) {
			 $models = $models->where('a.subject_id',$param['subject_id']);
		}
	
		
			$product=$models 
					->alias('a')
				    ->join('shoptype b','a.typeid=b.id')
				    ->join('shopsubject c','a.subject_id=c.id')
					->join('shopcategory d','a.category_id=d.id')
					->field('a.*,b.name as type,c.title as subject,d.title as category')
					->where(['a.status'=>1])
					->order('a.create_time desc,a.id desc')->paginate([
						    'query' => $param,
			                'list_rows'=>$this->request->param('limit',20)
			        ]);
		
		
		//分类
		$type=Db::name('shoptype')->where('status',1)->order('weigh asc,create_time desc')->select()->toArray();
		// 类别
		$category=Db::name('shopcategory')->where('status',1)->order('weigh asc,create_time desc')->select()->toArray();
		// 学科
		$subject=Db::name('shopsubject')->where('status',1)->order('weigh asc,create_time desc')->select()->toArray();
		// print_r($type);exit;
		return view('category',compact('product','type','category','subject'));
		// return view('category',['product'=>$product,'info'=>$info,'category'=>$category,'subject'=>$subject]);
		// return view('category',compact('product'));
	}
    /**
     * 添加学校产品
     */
    public function add()
    {
		$admin=Session::get('admin_auth');
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = $this->model->save($param);
			
            if( $result ) {
                xn_add_admin_log('添加学校产品');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        return view('form');
    }

    /**
     * 编辑学校产品
     */
    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = $this->model->update($param);
            if( $result ) {
                xn_add_admin_log('编辑学校产品');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $id = $this->request->get('id');
        $_data = $this->model->find($id);
        $data = $_data->toArray();
        $data['home'] = $_data->getData('home');
		$datas = Db::name('product')->where(['id'=>$_data['product_id']])->find();
		$data['name']=$datas['name'];
		$data['image']=$datas['image'];
		if($datas['images']){
			$data['images']=explode(',',$datas['images']);
		}
		// print_r($data);exit;
        return view('form',compact('data'));
    }
    // 选取产品
	public function select()
	{
		$admin=Session::get('admin_auth');
		// print_r($admin);exit;
		$info=Db::name('admin')->where(['id'=>$admin['id']])->find();
		$id=$this->request->param('product_id');
		$select=Db::name('schoolproduct')->where(['product_id'=>$id,'platform_id'=>$info['type_id']])->find();
		if($select){
			$this->error('该产品已添加');
		}
	    if( $this->request->isPost() ) {
	        $param = $this->request->param();
	        if($info['user_type']==1){
				$param['type_id']=1;
			}else{
				$param['type_id']=2;
			}		
			$param['create_time']=time();
			$param['update_time']=time();
			$param['platform_id']=$info['type_id'];
			$result=Db::name('schoolproduct')->insert($param);
			xn_add_admin_log('选取'.$param['product_id'].'产品成功');
			$this->success('操作成功');
			
			
	    }
	    // $id = $this->request->get('id');
	    // $_data = $this->model->find($id);
	    $data = Db::name('product')->where(['id'=>$id])->find();
		if($data['images']){
			$data['images']=explode(',',$data['images']);
		}
	    // $data['home'] = $_data->getData('home');
	    return view('form',compact('data'));
	}
    /**
     * 删除学校产品
     */
    public function delete()
    {
        $id = $this->request->param('id');
        $this->model->destroy($id);
        xn_add_admin_log('删除学校产品');
        $this->success('删除成功');
    }
}